package com.example.yaseademo.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.yaseademo.R;
import com.example.yaseademo.bean.MessageData;

import java.util.List;

public class MessageAdapter extends BaseAdapter {
    List<MessageData> messageData;
    Context context;
    ViewHolder viewHolder;

    public MessageAdapter(List<MessageData> messageData, Context context) {
        this.messageData = messageData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return messageData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = View.inflate(context, R.layout.item_message, null);
        } else {
            view = convertView;
        }
        viewHolder = new ViewHolder(view);

        if (messageData.get(position).isOwn()) {
            viewHolder.tv_other.setVisibility(View.GONE);
            viewHolder.tv_own.setVisibility(View.VISIBLE);
            viewHolder.tv_own.setText(messageData.get(position).getMessage());
        } else {
            viewHolder.tv_other.setVisibility(View.VISIBLE);
            viewHolder.tv_own.setVisibility(View.GONE);
            viewHolder.tv_other.setText(messageData.get(position).getMessage());
        }
        return view;
    }

    static class ViewHolder {
        public View rootView;
        public TextView tv_own;
        public TextView tv_other;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_own = rootView.findViewById(R.id.tv_own);
            this.tv_other = rootView.findViewById(R.id.tv_other);
        }

    }
}
